package com.config.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.LineConfigDao;
import com.config.dao.ShiftConfigDao;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.entity.ShiftConfig;
import com.config.entity.TreeModel;


@Service(value="lineConfigService")
public class LineConfigServiceImpl implements LineConfigService{
	
	@Resource(name = "lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name = "shiftConfigDao")
	private ShiftConfigDao shiftConfigDao;

	@Override
	public void save(LineConfig entity) {
		lineConfigDao.save(entity);
	}

	@Override
	public void update(LineConfig entity) {
		lineConfigDao.update(entity);
	}

	@Override
	public void delete(int id) {
		lineConfigDao.delete(id);
	}

	@Override
	public LineConfig getLineConfig(int id) {
		return lineConfigDao.get(id);
	}

	@Override
	public List<LineConfig> findAllWorkshop() {
		return lineConfigDao.findAllWorkShop();
	}

	@Override
	public List<LineConfig> findAllLine() {
		return lineConfigDao.findAllLine();
	}

	@Override
	public List<LineConfig> findAll() {
		return lineConfigDao.findAll();
	}

	@Override
	public List<TreeModel> generateTree() {
		List<LineConfig> workShops = lineConfigDao.findAllWorkShop();
		List<TreeModel> trees = new ArrayList<TreeModel>();
		List<TreeModel> childrens = null;
		TreeModel tree = null;
		TreeModel t = null;
		for(LineConfig ws:workShops){
			tree = new TreeModel();
			tree.setId(ws.getId());
			tree.setIconCls("icon-p33");
			tree.setText(ws.getName());
			tree.setState("open");
			List<LineConfig> lines = lineConfigDao.findLineByWorkshop(ws.getId());
			if(lines!=null && lines.size()>0){
				childrens = new ArrayList<TreeModel>();
				for(LineConfig l:lines){
					t = new TreeModel();
					t.setId(l.getId());
					t.setIconCls("icon-p31");
					t.setText(l.getName());
					t.setState("open");
					t.setUrl(l.getId().toString());
					List<ShiftConfig> shifts=shiftConfigDao.findShiftConfigsByLine(l.getId());
					if(shifts!=null&&shifts.size()>0){
						t.setShifts("Y");
					}
					childrens.add(t);
				}
				tree.setChildren(childrens);
			}
			trees.add(tree);
		}
		return trees;
	}

	@Override
	public LineConfig findFirstLine() {
		return lineConfigDao.findFirstLine();
	}
	
	public List<LineConfig> listByWorkShop(Integer id) {
		return lineConfigDao.findLineByWorkshop(id);
	}

	@Override
	public List<LineConfig> findByTreePage(Integer id, int start, int limit) {
		List<LineConfig> structure=lineConfigDao.findByTreePage(id,start,limit);
		for(LineConfig e:structure){
			List<LineConfig> child=lineConfigDao.findLineByWorkshop(e.getId());
			if(child.size()>0)
			e.setChildren(child);
		}
		return structure;
	}

	@Override
	public int countByTreePage(Integer id) {
		return lineConfigDao.countByTreePage(id);
	}

	@Override
	public LineConfig getByLineNo(String lineNo) {
		return lineConfigDao.getByLineNo(lineNo);
	}
	/**
	 * 根据生产线编号查找
	 * @param lineNo
	 * @return
	 */
	@Override
	public Map<String, Object> getAllLines() {
		Map<String,Object> lineComboBox=new HashMap<String,Object>();
		List<Map<String,String>> list=new ArrayList<Map<String,String>>();
		List<LineConfig>lines=lineConfigDao.findAllLine();
		if(lines.size()!=0){
			for(LineConfig line:lines){
				Map<String,String> lmap=new HashMap<String,String>();
				lmap.put("label", line.getName());
				lmap.put("value", line.getId().toString());
				list.add(lmap);
			}
		}
		lineComboBox.put("lineComboBox", list);
		return lineComboBox;
	}
}
